# -*- coding: utf-8 -*-

from django.shortcuts import render, HttpResponse
from django.views.generic.base import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from .models import ProjectShow
from courses.models import CourseCategory

# Create your views here.

class ProjectShowView(View):
    """
    项目展示
    """
    def get(self, request):
        all_category = CourseCategory.objects.all()

        click_category = request.GET.get("category", "all")
        click_sort = request.GET.get("sort", "new")

        if click_category == "all":
            all_project = ProjectShow.objects.all().order_by("-add_time")
            if click_sort == "hot":
                all_project = all_project.order_by("-students")
        else:
            all_project = ProjectShow.objects.filter(coursecategory__category=click_category).order_by("-add_time")
            if click_sort == "hot":
                all_project = all_project.order_by("-students")

        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_project, 6, request=request)
        projects = p.page(page)

        return render(request, "project_show.html", {
            "all_category":all_category,
            "click_sort":click_sort,
            "projects":projects,
            "click_category": click_category,
        })


class ProjectResultView(View):
    """
    项目展示结果
    """
    def get(self, request, project_id):
        print("lesson:", project_id)
        try:
            program_result = ProjectShow.objects.get(id=int(project_id))
        except ProjectShow.DoesNotExist:
            program_result = ProjectShow.objects.all()
        print(program_result)
        print(program_result.course.all())
        return render(request, "project_result.html", {
            "program_result":program_result
        })
